package com.shinbada.modules.competitor.service;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.shinbada.modules.competitor.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.rmi.ServerException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 88531
 */
@Service
public class CompetitorExportService {
    @Autowired
    private CompetitorProductService competitorProductService;

    @Autowired
    private CompetitorProductDataItemService competitorProductDataItemService;

    @Autowired
    private CompetitorProductDataItemValueService competitorProductDataItemValueService;

    @Autowired
    private CompetitorControlService competitorControlService;

    @Autowired
    private CompetitorControlDataItemService competitorControlDataItemService;

    @Autowired
    private CompetitorNoumenonService competitorNoumenonService;

    @Autowired
    private CompetitorNoumenonDataItemService competitorNoumenonDataItemService;

    @Autowired
    private CompetitorAdditionalService competitorAdditionalService;

    @Autowired
    private CompetitorControlAdditionalService competitorControlAdditionalService;

    @Autowired
    private CompetitorControlNoumenonService competitorControlNoumenonService;
    public static final String SPD_PRODUCT = "SPD产品";

    public static final String ISCB_PRODUCT = "ISCB产品";

    public static final String ATS_CONTROLLER = "ATS控制器";

    public static final String ATS_NOUMENON = "ATS本体";

    public static final String ADDITIONAL = "附加模块";

    public static final String NOUMENON_CONTROLLER = "本体与控制器关系";

    public static final String CONTROLLER_ADDTITIONAL = "控制器与附加模块关系";

    private Map<String, String> competitorControlMap;

    private Map<String, String> competitorNoumenonMap;

    private Map<String, String> competitorAdditionalMap;

    /**
     * 导入产品
     * @param file
     * @param index
     */
    public String productDataProcessing(MultipartFile file, Integer index) {
        if (file.isEmpty()) {
            new ServerException("文件不能为空!");
        }
        // 1.获取上传文件输入流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String msg;
        switch (index) {
            case 1:
                // ATS本体导入处理
                noumenonDataProcessing(inputStream);
                msg = "ATS本体导入完成";
                break;
            case 2:
                // ATS控制器导入处理
                controlDataProcessing(inputStream);
                msg = "ATS控制器导入完成";
                break;
            case 3:
                // SPD产品导入处理
                spdDataProcessing(inputStream);
                msg = "SPD产品导入完成";
                break;
            case 4:
                // ISCB产品导入处理
                iscbDataProcessing(inputStream);
                msg = "ISCB产品导入完成";
                break;
            case 5:
               // 附加模块导入处理
               additionalDataProcessing(inputStream);
               msg = "附加模块导入完成";
               break;
            case 6:
               // 本体与控制关系导入处理
               controlAndNoumenonProcessing(inputStream);
               msg = "本体与控制关系导入完成";
               break;
            case 7:
               // 控制器附加模块关系导入处理
               controlAndAdditionalDataProcessing(inputStream);
               msg = "控制器附加模块关系导入完成";
               break;
            case 8:
               // 暂无模板设置
               //controlDataProcessing(inputStream);
               msg = "暂无模板设置";
               break;
            default:
               msg = "请输入导入索引";
               break;
        }
        return msg;
    }

    /**
     * 控制器附加模块关系导入处理
     * @param inputStream
     */
    private void controlAndAdditionalDataProcessing(InputStream inputStream) {
        // 获取数据集
        List<CompetitorControl> competitorControlList = competitorControlService.findAllList(new CompetitorControl());
        List<CompetitorAdditional> competitorAdditionalList = competitorAdditionalService.findAllList(new CompetitorAdditional());
        this.competitorControlMap  = competitorControlList.stream().collect(Collectors.toMap(CompetitorControl::getModel, CompetitorControl::getId));
        this.competitorAdditionalMap  = competitorAdditionalList.stream().collect(Collectors.toMap(CompetitorAdditional::getName, CompetitorAdditional::getId));
        // 1.应用Hutool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, CONTROLLER_ADDTITIONAL);
        // 可以加上表头验证
        // 2.读取第二行到最后一行数据
        List<Map<String, Object>> read = excelReader.readAll();
        // 进行赋值 添加
        for (Map<String, Object> map : read) {
            // 1.根据产品名称产品系列 获取该产品信息 进行添加
            CompetitorControlAdditional competitorControlAdditional = new CompetitorControlAdditional();
            competitorControlAdditional.setControlId(this.competitorControlMap.get(map.get("控制器型号").toString()));
            competitorControlAdditional.setAdditionalId(this.competitorAdditionalMap.get(map.get("附加模块名称").toString()));
            competitorControlAdditionalService.save(competitorControlAdditional);
        }
    }

    /**
     * 本体与控制关系导入处理
     * @param inputStream
     */
    private void controlAndNoumenonProcessing(InputStream inputStream) {
        // 获取数据集
        List<CompetitorControl> competitorControlList = competitorControlService.findAllList(new CompetitorControl());
        List<CompetitorNoumenon> competitorNoumenonList = competitorNoumenonService.findAllList(new CompetitorNoumenon());
        this.competitorControlMap  = competitorControlList.stream().collect(Collectors.toMap(CompetitorControl::getModel, CompetitorControl::getId));
        this.competitorNoumenonMap  = competitorNoumenonList.stream().collect(Collectors.toMap(CompetitorNoumenon::getModel, CompetitorNoumenon::getId));
        // 1.应用Hutool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, NOUMENON_CONTROLLER);
        // 可以加上表头验证
        // 2.读取第二行到最后一行数据
        List<Map<String, Object>> read = excelReader.readAll();
        // 进行赋值 添加
        for (Map<String, Object> map : read) {
            // 1.根据产品名称产品系列 获取该产品信息 进行添加
            CompetitorControlNoumenon competitorControlNoumenon = new CompetitorControlNoumenon();
            competitorControlNoumenon.setControlId(this.competitorControlMap.get(map.get("控制器型号").toString()));
            competitorControlNoumenon.setNoumenonId(this.competitorNoumenonMap.get(map.get("本体型号").toString()));
            competitorControlNoumenonService.save(competitorControlNoumenon);
        }
    }

    /**
     * 附加模块导入处理
     * @param inputStream
     */
    private void additionalDataProcessing(InputStream inputStream) {
        // 1.应用Hutool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, ADDITIONAL);
        // 可以加上表头验证
        // 2.读取第二行到最后一行数据
        List<Map<String, Object>> read = excelReader.readAll();
        // 进行附加模块赋值 添加
        for (Map<String, Object> map : read) {
            // 1.根据产品名称产品系列 获取该产品信息 进行添加
            CompetitorAdditional competitorAdditional = new CompetitorAdditional();
            competitorAdditional.setName(map.get("附加模块名称").toString());
            competitorAdditional.setDetail(map.get("详细内容").toString());
            competitorAdditionalService.save(competitorAdditional);
        }
    }

    /**
     * ATS 控制器导入
     * @param inputStream
     */
    @Transactional(readOnly = false)
    public void controlDataProcessing(InputStream inputStream) {

        // 2.应用Hutool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, ATS_CONTROLLER);
        // 可以加上表头验证
        // 3.读取第二行到最后一行数据
        List<Map<String, Object>> read = excelReader.readAll();
        // 获取sheet信息 根据sheet获取对应数据项
        // 根据添加得产品基础信息 进行数据项追加
        // 根据 系列 对该产品追加数据项
        // 进行赋值 添加
        for (Map<String, Object> map : read) {
            // 1.根据产品名称产品系列 获取该产品信息 进行添加
            CompetitorControl competitorControl = new CompetitorControl();
            competitorControl.setModel(map.get("控制器型号").toString());
            competitorControl.setAdaptive(map.get("控制器适配").toString());
            competitorControl.setPlatform(map.get("控制器平台").toString());
            competitorControlService.save(competitorControl);
            // 2.根据系列获取数据项
            List<CompetitorProductDataItem> competitorProductDataItems = competitorProductDataItemService.selectDataItemByType(CompetitorControl.ATS_CONTROLLER);
            // 根据数据项获取Excel数据
            for (CompetitorProductDataItem productDataItem : competitorProductDataItems) {
                // 根据数据项获取指定数据
                String dataItemValue = map.get(productDataItem.getName())+"";
                if (dataItemValue !=null && dataItemValue != ""){
                    // 进行存储
                    CompetitorControlDataItem competitorControlDataItem = new CompetitorControlDataItem();
                    competitorControlDataItem.setDataItemId(productDataItem.getId());
                    competitorControlDataItem.setControlId(competitorControl.getId());
                    competitorControlDataItem.setControlValue(dataItemValue);
                    competitorControlDataItemService.save(competitorControlDataItem);
                }

            }

            System.out.println(map);
        }
    }

    /**
     * ATS本体数据导入
     * @param inputStream
     */
    @Transactional(readOnly = false)
    public void noumenonDataProcessing(InputStream inputStream) {
        // 2.应用Hutool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, ATS_NOUMENON);
        // 可以加上表头验证
        // 3.读取第二行到最后一行数据
        List<Map<String, Object>> read = excelReader.readAll();
        // 获取sheet信息 根据sheet获取对应数据项
        // 根据添加得产品基础信息 进行数据项追加
        // 根据 系列 对该产品追加数据项
        // 进行赋值 添加
        for (Map<String, Object> map : read) {
            // 1.根据本体产品系列 获取该本体 进行添加
            CompetitorNoumenon competitorNoumenon = new CompetitorNoumenon();
            competitorNoumenon.setModel(map.get("本体型号").toString());
            competitorNoumenon.setBrand(map.get("品牌").toString());
            competitorNoumenon.setSeries(map.get("产品系列").toString());
            competitorNoumenon.setChiefApplication(map.get("产品主要应用").toString());
            competitorNoumenonService.save(competitorNoumenon);
            // 2.根据系列获取数据项
            List<CompetitorProductDataItem> competitorProductDataItems = competitorProductDataItemService.selectDataItemByType(CompetitorNoumenon.ATS_NOUMENON);
            // 根据数据项获取Excel数据
            for (CompetitorProductDataItem productDataItem : competitorProductDataItems) {
                // 根据数据项获取指定数据
                String dataItemValue = map.get(productDataItem.getName())+"";
                if (dataItemValue !=null && dataItemValue != ""){
                    // 进行存储
                    CompetitorNoumenonDataItem competitorNoumenonDataItem = new CompetitorNoumenonDataItem();
                    competitorNoumenonDataItem.setDataItemId(productDataItem.getId());
                    competitorNoumenonDataItem.setNoumenonId(competitorNoumenon.getId());
                    competitorNoumenonDataItem.setNoumenonValue(dataItemValue);
                    competitorNoumenonDataItemService.save(competitorNoumenonDataItem);
                }
            }

            System.out.println(map);
        }
    }

    /**
     * spd产品导入
     * @param inputStream
     */
    @Transactional(readOnly = false)
    public void spdDataProcessing(InputStream inputStream) {
        // 2.应用Hutool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream,SPD_PRODUCT);
        // 可以加上表头验证
        // 3.读取第二行到最后一行数据
        List<Map<String, Object>> read = excelReader.readAll();
        // 获取sheet信息 根据sheet获取对应数据项
        // 根据添加得产品基础信息 进行数据项追加
        // 根据 系列 对该产品追加数据项
        // 进行赋值 添加
        for (Map<String, Object> map : read) {
            // 1.根据产品名称产品系列 获取该产品信息 进行添加
            CompetitorProduct competitorProduct = new CompetitorProduct();
            competitorProduct.setProductModel(map.get("产品型号").toString());
            competitorProduct.setProductBrand(map.get("品牌").toString());
            competitorProduct.setProductSeries(map.get("产品系列").toString());
            competitorProduct.setProductType(CompetitorProduct.SPD_TYPE);
            competitorProductService.save(competitorProduct);
            // 2.根据系列获取数据项
            List<CompetitorProductDataItem> competitorProductDataItems = competitorProductDataItemService.selectDataItemByType(CompetitorProduct.SPD_TYPE);
            // 根据数据项获取Excel数据
            for (CompetitorProductDataItem productDataItem : competitorProductDataItems) {
                // 根据数据项获取指定数据
                String dataItemValue = map.get(productDataItem.getName())+"";
                if (dataItemValue != null && dataItemValue != ""){
                    // 进行存储
                    CompetitorProductDataItemValue competitorProductDataItemValue = new CompetitorProductDataItemValue();
                    competitorProductDataItemValue.setDataItemId(productDataItem.getId());
                    competitorProductDataItemValue.setProductId(competitorProduct.getId());
                    competitorProductDataItemValue.setDataItemValue(dataItemValue);
                    competitorProductDataItemValueService.save(competitorProductDataItemValue);
                }
            }

            System.out.println(map);
        }
    }

    /**
     * iscb产品导入
     * @param inputStream
     */
    @Transactional(readOnly = false)
    public void iscbDataProcessing(InputStream inputStream) {
        // 2.应用Hutool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream,ISCB_PRODUCT);
        // 可以加上表头验证
        // 3.读取第二行到最后一行数据
        List<Map<String, Object>> read = excelReader.readAll();
        // 获取sheet信息 根据sheet获取对应数据项
        // 根据添加得产品基础信息 进行数据项追加
        // 根据 系列 对该产品追加数据项
        // 进行赋值 添加
        for (Map<String, Object> map : read) {
            // 1.根据产品名称产品系列 获取该产品信息 进行添加
            CompetitorProduct competitorProduct = new CompetitorProduct();
            competitorProduct.setProductModel(map.get("产品型号").toString());
            competitorProduct.setProductBrand(map.get("品牌").toString());
            competitorProduct.setProductSeries(map.get("产品系列").toString());
            competitorProduct.setProductType(CompetitorProduct.ISCB_TYPE);
            competitorProductService.save(competitorProduct);
            // 2.根据系列获取数据项
            List<CompetitorProductDataItem> competitorProductDataItems = competitorProductDataItemService.selectDataItemByType(CompetitorProduct.ISCB_TYPE);
            // 根据数据项获取Excel数据
            for (CompetitorProductDataItem productDataItem : competitorProductDataItems) {
                // 根据数据项获取指定数据
                String dataItemValue = map.get(productDataItem.getName())+"";
                if (dataItemValue != null && dataItemValue != "") {
                    // 进行存储
                    CompetitorProductDataItemValue competitorProductDataItemValue = new CompetitorProductDataItemValue();
                    competitorProductDataItemValue.setDataItemId(productDataItem.getId());
                    competitorProductDataItemValue.setProductId(competitorProduct.getId());
                    competitorProductDataItemValue.setDataItemValue(dataItemValue);
                    competitorProductDataItemValueService.save(competitorProductDataItemValue);
                }
            }

            System.out.println(map);
        }
    }


}
